//
//  Asset.m
//
//  Created by Matt Tuzzolo on 2/15/11.
//  Copyright 2011 ELC Technologies. All rights reserved.
//

#import "ELCAsset.h"
#import "ELCAssetTablePicker.h"

#import "PublicMethodEngine.h"
#import "SDDataCache.h"

@implementation ELCAsset

@synthesize asset;
@synthesize parent;

- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        // Initialization code
    }
    return self;
}

-(id)initWithAsset:(ALAsset*)_asset {
	
	if (self = [super initWithFrame:CGRectMake(0, 0, 0, 0)]) {
		
		self.asset = _asset;
		
		CGRect viewFrames = CGRectMake(0, 0, 75, 75);
		
		UIImageView *assetImageView = [[UIImageView alloc] initWithFrame:viewFrames];
		[assetImageView setContentMode:UIViewContentModeScaleToFill];
		[assetImageView setImage:[UIImage imageWithCGImage:[self.asset thumbnail]]];
		[self addSubview:assetImageView];
		[assetImageView release];
		
		overlayView = [[UIImageView alloc] initWithFrame:viewFrames];
		[overlayView setImage:[UIImage imageNamed:@"Overlay.png"]];
		[overlayView setHidden:YES];
		[self addSubview:overlayView];
    }
    
	return self;	
}

-(void)toggleSelection {

    float freeDisk = [[PublicMethodEngine sharedPublicMethodEngine] getFreeDiskspaceMiB];
    
    CLog(@"Free disk ： %f!", freeDisk);
    
    if (freeDisk < 25.0) {
        [LoadingView showImage:[UIImage imageNamed:@"wangluo_iphone.png"] info:@"剩余磁盘空间较小，建议您整理设备空间后，再继续上传！" duration:DEFAULT_LOADING_TIME *1.5];
        
        return;
    }

    NSInteger maxSize = 20;
    //限制最大选择数量
    if([(ELCAssetTablePicker*)self.parent totalSelectedAssets] >= maxSize) {
        
        [LoadingView showImage:[UIImage imageNamed:@"wangluo_iphone.png"] info:[NSString stringWithFormat:@"每次最多上传%d张照片！", maxSize]  duration:DEFAULT_LOADING_TIME];

        return;
    }
    
    overlayView.hidden = !overlayView.hidden;
    
    ELCAssetTablePicker *tablePicker  = (ELCAssetTablePicker*)self.parent ;
    
    if (tablePicker.selAllButton && tablePicker.selAllButton.tag == 0 && overlayView.hidden) {
        [tablePicker cancleSelAll];
    }
    
    CLog(@"ToggleSelection");
    
    
    NSString *appendKey = [tablePicker getAppendKey];
    CLog(@"appendkey %@", appendKey);
    //保存到缓存
    if (!overlayView.hidden) {
        CLog(@"asset %@", asset);
        
        NSURL *url = [[self.asset valueForProperty:ALAssetPropertyURLs] valueForKey:[[[asset valueForProperty:ALAssetPropertyURLs] allKeys] objectAtIndex:0]];
        NSString *urlStr = [NSString stringWithFormat:@"%@%@", appendKey, [url absoluteString]];
        
        if (urlStr) {
            UIImage *fullImage = [UIImage imageWithCGImage:[[self.asset defaultRepresentation] fullScreenImage]];
            
            NSData *imageData = [[PublicMethodEngine sharedPublicMethodEngine] compresImageDataWith:fullImage maxPix:PHOTO_MAXPIX];
            
            [[SDDataCache sharedDataCache:kImageCache_Locale_Album] storeData:imageData forKey:urlStr];
        }

    }
    else {
        NSURL *url = [[self.asset valueForProperty:ALAssetPropertyURLs] valueForKey:[[[asset valueForProperty:ALAssetPropertyURLs] allKeys] objectAtIndex:0]];
        NSString *urlStr = [NSString stringWithFormat:@"%@%@", appendKey, [url absoluteString]];

        if (urlStr) {
            [[SDDataCache sharedDataCache:kImageCache_Locale_Album] removeDataForKey:urlStr];
        }
        
    }
    
}

-(BOOL)selected {
	
	return !overlayView.hidden;
    
}

-(void)setSelected:(BOOL)_selected {
    CLog(@"selected");
    
	[overlayView setHidden:!_selected];
    if (_selected) {
        NSString *urlStr = [[asset valueForProperty:ALAssetPropertyURLs] valueForKey:[[[asset valueForProperty:ALAssetPropertyURLs] allKeys] objectAtIndex:0]];
        
        UIImage *fullImage = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]];
        
        NSData *imageData = [[PublicMethodEngine sharedPublicMethodEngine] compresImageDataWith:fullImage maxPix:PHOTO_MAXPIX];
        
        [[SDDataCache sharedDataCache:kImageCache_Locale_Album] storeData:imageData forKey:urlStr];
    }
    else {
        NSString *urlStr = [[asset valueForProperty:ALAssetPropertyURLs] valueForKey:[[[asset valueForProperty:ALAssetPropertyURLs] allKeys] objectAtIndex:0]];
        
        [[SDDataCache sharedDataCache:kImageCache_Locale_Album] removeDataForKey:urlStr];
    }
    
}

- (void)dealloc 
{    
    self.asset = nil;
	[overlayView release];
    [super dealloc];
}

@end

